from PyQt4.QtCore import *
from PyQt4.QtGui import *

from summary import Ui_Dialog
import layerItem

import opsR
import displayText

class Dialog(QDialog, Ui_Dialog):
    def __init__(self, iface):
        QDialog.__init__(self)
        self.iface=iface
        # Set up the user interface from Designer.
        self.setupUi(self)
        
        # when layer list changes, update attribute list

        QObject.connect(self.layerList, SIGNAL("currentItemChanged(QListWidgetItem*, QListWidgetItem*)"), self.changed)
        # populate layer list
        mapCanvas=self.iface.mapCanvas()
        for i in range(mapCanvas.layerCount()):
            layer = mapCanvas.layer(i)
            if layer.type() == layer.VectorLayer:
                w = layerItem.layerItem(layer)
                self.layerList.addItem(w)
            
    def changed(self, curr, prev):
        layer = curr.layer
        self.attributeList.clear()
        provider = layer.dataProvider()
        fields = provider.fields()
        [self.attributeList.addItem(layerItem.fieldItem(x, layer)) for x in fields.values()]
        
    def accept(self):
        items=self.attributeList.selectedItems()
        data={}
        for field in items:
            data[str(field.fieldName())] = field.getData()
        s = opsR.dictSummary(data)
        displayText.show(s)
        


